package com.meiyou.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.meiyou.mapper.MeiyouUserMapper;
import com.meiyou.pojo.MeiyouUser;
import com.meiyou.service.TecentService;
import com.meiyou.utils.Context;
import com.meiyou.utils.Msg;
import com.tls.tls_sigature.tls_sigature;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@Api(value="腾讯云有关控制器",tags="腾讯云")
public class TencentController {

	@Autowired
	TecentService tencentService;
	@Autowired
	MeiyouUserMapper userMapper;
	
	@RequestMapping(value="addServiceMan")
	@ApiOperation("添加客服")
	@ResponseBody
	public Msg addServiceMan(String count,String name,String sex) {
		Msg msg;
		MeiyouUser user = new MeiyouUser();
		user.setuCount(count);
		user.setuId(22);
		user.setuName(name);
		user.setuImg("https://www.meiyou8.cn/meiyou/imgs/uimg/serviceman.jpg");
		user.setuSex("男");
		if(tencentService.registTencent(user)) {
			msg = Msg.success();
			return msg;
		}else {
			msg = Msg.fail();
			return msg;
		}
	}
	
	@RequestMapping(value = "sendMoney", method = RequestMethod.POST)
	@ApiOperation("发送红包")
	public @ResponseBody Msg sendMoney(HttpServletRequest req,String toCount,Double meijin) {
		Msg msg;
		
		if(req.getSession().getAttribute("user")!=null) {
			String fromucount = req.getSession().getAttribute("user").toString();
			return tencentService.sendHongbao(fromucount,toCount, meijin);
		}else {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
		
	}
	
     /**
      * 获取usersig
      */
	@RequestMapping(value = "getSig", method = RequestMethod.POST)
	@ApiOperation("获取用户sig")
	public @ResponseBody Msg getSig(HttpServletRequest req) {
		Msg msg;
		if (req.getSession().getAttribute("user") != null) {
			String usersig = tls_sigature.genSig(Context.SDKAPPID, req.getSession().getAttribute("user").toString(), Context.PRIKEY).urlSig;
			msg = Msg.success();
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("userSig",usersig);
			msg.setExtend(map);
			return msg;
		}else {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}	
	}
	
	/**
	 * 发送文本消息
	 * @param formucount
	 * @param toucount
	 * @param context
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "sendTextMsg", method = RequestMethod.POST)
	@ApiOperation("发送文本消息")
	public @ResponseBody Msg sendMsg(String toucount, String context, HttpServletRequest req) {

		Msg msg;
		if (req.getSession().getAttribute("user") != null) {
			if (tencentService.userSendMsg(req.getSession().getAttribute("user").toString(), toucount, context, 2)) {
				msg = Msg.success();
				return msg;
			} else {
				msg = Msg.fail();
				return msg;
			}
		}else {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
	}
	
	/**
	 * 发送表情包消息
	 * @param formucount
	 * @param toucount
	 * @param context
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "sendBiaoQingMsg", method = RequestMethod.POST)
	@ApiOperation("发送表情包消息")
	public @ResponseBody Msg sendBiaoQingMsg(String toucount, int index, HttpServletRequest req) {

		Msg msg;
		if (req.getSession().getAttribute("user") != null) {
			if (tencentService.userSendBiaoQingMsg(req.getSession().getAttribute("user").toString(), toucount, index, 2)) {
				msg = Msg.success();
				return msg;
			} else {
				msg = Msg.fail();
				return msg;
			}
		}else {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
		
	}
    /**
     * 发送位置消息
     * @param fromUser
     * @param toUser
     * @param desc
     * @param latitude
     * @param longitude
     * @param req
     * @return
     */
	@RequestMapping(value = "sendWeiZhiMsg", method = RequestMethod.POST)
	@ApiOperation("发送位置消息")
	public @ResponseBody Msg sendWeiZhiMsg( String toUser, String desc, double latitude, double longitude, HttpServletRequest req) {

		Msg msg;
		if (req.getSession().getAttribute("user") != null) {
			if (tencentService.userSendWeiZhiMsg(req.getSession().getAttribute("user").toString(), toUser, desc, latitude, longitude, 2)) {
				msg = Msg.success();
				return msg;
			} else {
				msg = Msg.fail();
				return msg;
			}
		}else {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
	}
	
	/**
	 * 添加好友
	 * @param fromUser
	 * @param toUser
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "addFriend", method = RequestMethod.POST)
	@ApiOperation("添加好友")
	public @ResponseBody Msg addFriend(String toUser, HttpServletRequest req) {

		Msg msg;
		if (req.getSession().getAttribute("user") != null) {
			if (tencentService.addFriend(req.getSession().getAttribute("user").toString(), toUser)) {
				msg = Msg.success();
				return msg;
			} else {
				msg = Msg.fail();
				return msg;
			}
		}else {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
	}
	
	
	/**
	 * 删除好友
	 * @param fromUser
	 * @param toUser
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "delFriend", method = RequestMethod.POST)
	@ApiOperation("删除好友")
	public @ResponseBody Msg delFriend(String toUser, HttpServletRequest req) {

		Msg msg;
		if (req.getSession().getAttribute("user") != null) {
			if (tencentService.deleteFriend(req.getSession().getAttribute("user").toString(), toUser)) {
				msg = Msg.success();
				return msg;
			} else {
				msg = Msg.fail();
				return msg;
			}
		}else {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
	}
	
	/**
	 * 添加黑名单
	 * @param fromUser
	 * @param toUser
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "addBlackList", method = RequestMethod.POST)
	@ApiOperation("添加黑名单")
	public @ResponseBody Msg addBlackList(String toUser, HttpServletRequest req) {

		Msg msg;
		if (req.getSession().getAttribute("user") != null) {
			if (tencentService.addBlackList(req.getSession().getAttribute("user").toString(), toUser)) {
				msg = Msg.success();
				return msg;
			} else {
				msg = Msg.fail();
				return msg;
			}
		}else {
			msg = Msg.fail();
			msg.setMsg("请先登录");
			return msg;
		}
	}
}
